/*
 * UsersParser.fx
 *
 * Created on 18.2.2010, 23:46:
 */
package com.gpsbuddy.services;

import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import java.io.InputStream;

/**
 * @author jonycus
 */
public class UsersParser {

    public var errorMessage = "";

    public function parse(input: InputStream): User[] {
        // To hold all flights and currently processed flight
        var users: User[];
        var user: User;
        // Parse the input flight data (XML) and instantiate Flight object
        def parser = PullParser {
                    input: input
                    onEvent: function (event: Event) {
                        if (event.type == PullParser.START_ELEMENT) {
                            if (event.qname.name == "user" and event.level == 1) {
                                user = User { };
                            }
                        } else if (event.type == PullParser.END_ELEMENT) {
                            if (event.qname.name == "user" and event.level == 1) {
                                insert user into users;
                            }
                        } else if (event.type == PullParser.TEXT) {
                            if (event.qname.name == "email" and event.level == 2) {
                                user.email = event.text;
                            }
                            if (event.qname.name == "username" and event.level == 2) {
                                user.username = event.text;
                            }
                            if (event.qname.name == "password" and event.level == 2) {
                                user.password = event.text;
                            }
                            if (event.qname.name == "id" and event.level == 2) {
                                user.id = Integer.parseInt(event.text);
                            }
                            if (event.qname.name == "name" and event.level == 2) {
                                user.name = event.text;
                            }
                            if (event.qname.name == "surname" and event.level == 2) {
                                user.surname = event.text;
                            }
                        }
                    }
                }
        parser.parse();
        return users;
    }

}
